package game.view 
{
	import flash.display.Sprite;
	import game.events.EventName;
	import game.events.GameDispatcher;
	import game.manager.LayerManager;
	
	/**
	 * ...
	 * @author gcxk721
	 */
	public class GameView extends Sprite 
	{
		
		private static var instance:GameView;

        private var hideLayerList:Array;

        public function GameView()
		{
			if (instance != null)
			{
				throw new Error("single instance");
			}
            this.hideLayerList = [];
            super();
            this.init();
        }
		
		public function init():void
		{
            this.mouseEnabled = false;
            LayerManager.bgLayer = new Sprite();
            LayerManager.bgLayer.mouseEnabled = false;
            addChild(LayerManager.bgLayer);
            LayerManager.sceneLayer = new Sprite();
            LayerManager.sceneLayer.mouseEnabled = false;
            addChild(LayerManager.sceneLayer);
            LayerManager.sceneEffectLayer = new Sprite();
            LayerManager.sceneEffectLayer.mouseEnabled = false;
            addChild(LayerManager.sceneEffectLayer);
            LayerManager.sceneUILayer = new Sprite();
            LayerManager.sceneUILayer.mouseEnabled = false;
            addChild(LayerManager.sceneUILayer);
            LayerManager.imageLayer = new Sprite();
            LayerManager.imageLayer.mouseEnabled = false;
            addChild(LayerManager.imageLayer);
            LayerManager.iconLayer = new Sprite();
            LayerManager.iconLayer.mouseEnabled = false;
            addChild(LayerManager.iconLayer);
            LayerManager.dialogLayer = new Sprite();
            LayerManager.dialogLayer.mouseEnabled = false;
            addChild(LayerManager.dialogLayer);
            LayerManager.uiLayer = new Sprite();
            LayerManager.uiLayer.mouseEnabled = false;
            addChild(LayerManager.uiLayer);
            LayerManager.affinityLayer = new Sprite();
            LayerManager.affinityLayer.mouseEnabled = false;
            addChild(LayerManager.affinityLayer);
            LayerManager.msgLayer = new Sprite();
            LayerManager.msgLayer.mouseEnabled = false;
            addChild(LayerManager.msgLayer);
            LayerManager.buttomNoticeLayer = new Sprite();
            LayerManager.buttomNoticeLayer.mouseEnabled = false;
            LayerManager.buttomNoticeLayer.mouseChildren = false;
            addChild(LayerManager.buttomNoticeLayer);
            LayerManager.windowLayer = new Sprite();
            LayerManager.windowLayer.mouseEnabled = false;
            addChild(LayerManager.windowLayer);
            LayerManager.goodsInfoLayer = new Sprite();
            LayerManager.goodsInfoLayer.mouseEnabled = false;
            addChild(LayerManager.goodsInfoLayer);
            LayerManager.mallLayer = new Sprite();
            addChild(LayerManager.mallLayer);
            LayerManager.noticeLayer = new Sprite();
            LayerManager.noticeLayer.mouseEnabled = false;
            LayerManager.noticeLayer.mouseChildren = false;
            addChild(LayerManager.noticeLayer);
            LayerManager.noticeTextLayer = new Sprite();
            LayerManager.noticeTextLayer.mouseEnabled = false;
            addChild(LayerManager.noticeTextLayer);
            LayerManager.storyLayer = new Sprite();
            addChild(LayerManager.storyLayer);
            LayerManager.resLoadingLayer = new Sprite();
            addChild(LayerManager.resLoadingLayer);
            LayerManager.loginLayer = new Sprite();
            addChild(LayerManager.loginLayer);
            LayerManager.loadingLayer = new Sprite();
            addChild(LayerManager.loadingLayer);
            LayerManager.HelpsLayer = new Sprite();
            LayerManager.HelpsLayer.mouseEnabled = false;
            addChild(LayerManager.HelpsLayer);
            LayerManager.rightTipsLayer = new Sprite();
            LayerManager.rightTipsLayer.mouseEnabled = false;
            addChild(LayerManager.rightTipsLayer);
            LayerManager.tipsLayer = new Sprite();
            LayerManager.tipsLayer.mouseEnabled = false;
            LayerManager.tipsLayer.mouseChildren = false;
            addChild(LayerManager.tipsLayer);
            LayerManager.tipsTop = new Sprite();
            addChild(LayerManager.tipsTop);
            LayerManager.AccessGoods = new Sprite();
            LayerManager.AccessGoods.mouseChildren = false;
            LayerManager.AccessGoods.mouseEnabled = false;
            addChild(LayerManager.AccessGoods);
            LayerManager.loverFlower = new Sprite();
            addChild(LayerManager.loverFlower);
            LayerManager.topBtns = new Sprite();
            addChild(LayerManager.topBtns);
            LayerManager.topLayer = new Sprite();
            LayerManager.topLayer.mouseEnabled = false;
            addChild(LayerManager.topLayer);
            LayerManager.effectLayer = new Sprite();
            LayerManager.effectLayer.mouseEnabled = false;
            LayerManager.effectLayer.mouseChildren = false;
            addChild(LayerManager.effectLayer);
            LayerManager.mouseLayer = new Sprite();
            LayerManager.mouseLayer.mouseEnabled = false;
            LayerManager.mouseLayer.mouseChildren = false;
            addChild(LayerManager.mouseLayer);
            GameDispatcher.getInstance().addEventListener(EventName.STORY_TRIGGER, this.hideUI);
            GameDispatcher.getInstance().addEventListener(EventName.STORY_END, this.showUI);
            GameDispatcher.getInstance().addEventListener(EventName.LOCK_MOUSE, this.lockUI);
        }
		
		private function hideUI(evt : ParamEvent) : void
		{
            this.pushHideList(LayerManager.sceneUILayer);
            this.pushHideList(LayerManager.noticeLayer);
            this.pushHideList(LayerManager.dialogLayer);
            this.pushHideList(LayerManager.uiLayer);
            this.pushHideList(LayerManager.windowLayer);
            this.pushHideList(LayerManager.buttomNoticeLayer);
            this.pushHideList(LayerManager.goodsInfoLayer);
            this.pushHideList(LayerManager.HelpsLayer);
            this.pushHideList(LayerManager.rightTipsLayer);
            this.pushHideList(LayerManager.tipsLayer);
            this.pushHideList(LayerManager.tipsTop);
            this.pushHideList(LayerManager.msgLayer);
            this.pushHideList(LayerManager.AccessGoods);
            this.pushHideList(LayerManager.loverFlower);
            this.pushHideList(LayerManager.topBtns);
            this.pushHideList(LayerManager.topLayer);
            if (evt.param.type == 2)
			{
                this.pushHideList(LayerManager.iconLayer);
                this.pushHideList(LayerManager.mallLayer);
            }
        }
		
        private function showUI(evt : ParamEvent):void
		{
            var sprite : Sprite;
            while (this.hideLayerList.length > 0) 
			{
                sprite = this.hideLayerList.pop();
                sprite.visible = true;
            }
            //sprite = null;
        }
		
        private function pushHideList(sprite : Sprite) : void
		{
            if (sprite != null && sprite.visible)
			{
                sprite.visible = false;
                this.hideLayerList.push(sprite);
            }
        }
		
        private function lockUI(evt : ParamEvent) : void
		{
            this.mouseEnabled = !evt.param;
            this.mouseChildren = !evt.param;
        }
		
        public static function getInstance():GameView
		{
            if (instance == null)
			{
                instance = new GameView();
            }
            return instance;
        }
		
	}

}